#!/bin/sh
#
# Name: nWeb Script
# Version: 1.50
# Description: Install and configures Nginx with PHP support and MySQL.
# Script_URI: http://thehook.eu/tools/nweb/
#
# Author: Andre Hook
# Author URI: http://thehook.eu/
#


###########################
##### Run some checks #####
###########################

# Check if user is root
if [ $(id -u) != "0" ]; then
    echo "Error: You must be root to run this script, use sudo sh $0"
    exit 1
fi

# Check what version it is running on
DEBVER=$(cat /etc/debian_version)
RELEASE=$(lsb_release -r|awk '{print $2}')
if [ "$1" = "-nocheck" ]; then
	echo "OS Check disabled, let us continue.."
elif [ "$RELEASE" = "8.10" ]; then
	echo "You are running Ubuntu (8.10), let us continue.."
elif [ "$RELEASE" = "9.04" ]; then
	echo "You are running Ubuntu (9.04), let us continue.."
elif [ "$DEBVER" = "5.0" ]; then
	echo "Your are running Debian 5.0 (Lenny), let us continue.."
elif [ "$DEBVER" = "5.0.1" ]; then
	echo "Your are running Debian 5.0.1 (Lenny), let us continue.."
elif [ "$DEBVER" = "5.0.2" ]; then
	echo "Your are running Debian 5.0.2 (Lenny), let us continue.."
elif [ "$DEBVER" = "5.0.3" ]; then
	echo "Your are running Debian 5.0.3 (Lenny), let us continue.."
else
	echo "This script only supports the following Ubuntu versions:"
	echo " - Ubuntu 8.04 (Hardy Heron)"
	echo " - Ubuntu 8.10 (Intrepid Ibex)"
	echo " - Ubuntu 9.04 (Jaunty Jackalope)"
	echo "To disable this check, use sudo sh $0 -nocheck"
	exit 1
fi



#############################################
##### Set some variables and functions ######
#############################################

# Directory where this script is located
BASEDIR="$(dirname $0)"

error_out() {
    echo "Could not ${1}, aborting"
    exit 1
}

wrap() {
    local ErrorMsg="$1"
    shift
    local cmd="$1"
    shift
    $cmd "$@" || error_out "$ErrorMsg"
}

# Function to install packages
install_pkgs() {
    aptitude -yq=3 install "$@"
}

# Function to restart nginx, in some Ubuntu versions restart seems to be broken
restart_nginx() {
    /etc/init.d/nginx stop
    /etc/init.d/nginx start
}



#################################
##### The magic starts here #####
#################################

# Update package database
wrap "update package database" aptitude update

# Install Nginx, PHP and extensions
wrap "install nginx webserver" install_pkgs nginx
wrap "install PHP and it's extensions" install_pkgs php5-cgi php5-mysql php5-curl php5-gd php5-idn php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-mhash php5-ming php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl
wrap "fix cgi options" echo cgi.fix_pathinfo = 1 >> /etc/php5/cgi/php.ini

# Download and compile spawn-fcgi
wrap "install some essentials" install_pkgs build-essential
wrap "download spawn-fcgi source" wget http://www.lighttpd.net/download/spawn-fcgi-1.6.2.tar.gz
wrap "unpack source" tar xvfz spawn-fcgi-1.6.2.tar.gz
wrap "configure spawn-fcgi source" cd spawn-fcgi-1.6.2 && ./configure
wrap "make spawn-fcgi" make

#Install, setup and start spawn-fcgi
wrap "copy spawn-fcgi to system" cp ./src/spawn-fcgi /usr/local/bin/spawn-fcgi && cd ..
wrap "backup rc.local" cp /etc/rc.local /etc/rc.local.dist
wrap "add spawn-fcgi to startup" sed -i 's/exit 0/\/usr\/local\/bin\/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -g www-data -C 10 -f \/usr\/bin\/php5-cgi -P \/var\/run\/fastcgi-php.pid/g' /etc/rc.local
wrap "put back the exit 0 in rc.local" echo exit 0 >> /etc/rc.local
wrap "start spwawn-fcgi" /usr/local/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -g www-data -C 10 -f /usr/bin/php5-cgi -P /var/run/fastcgi-php.pid

# Configure nginx for using PHP through spawn-fcgi
wrap "backup old defaultsite" cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default.dist
wrap "updating default site" cp "${BASEDIR}/inc/defaultsite" /etc/nginx/sites-available/default
wrap "backup old nginx.conf" cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.dist
wrap "updating nginx.conf" cp "${BASEDIR}/inc/nginxconf" /etc/nginx/nginx.conf

# Restart nginx
wrap "restart nginx" restart_nginx

# Install MySQL Server
wrap "install MySQL server" install_pkgs mysql-server


clear
echo "#############################"
echo "##### Mission complete! #####"
echo "#############################"
echo "Visit http://ip-or-domain.tld to see if everything is working"
echo "Edit index.html in /var/www/nginx-default/ to reflect your catch-all webpage."
